package com.yjxz.shop.common.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;

public class JsonUtil {

    private JsonUtil() {
        throw new RuntimeException();
    }

    /**
     * Java Object -> Json
     * 
     * @param obj
     * @return
     */
    public static String toJson(Object obj) {
        return JSON.toJSONString(obj);
    }

    /**
     * Java Object（带时间） -> Json
     * 
     * @param obj
     * @return
     */
    public static String toJson(Object obj, String pattern) {
        return JSON.toJSONStringWithDateFormat(obj, pattern, SerializerFeature.WriteDateUseDateFormat);
    }

    /**
     * 根据JSON的属性读取对应的值
     * 
     * @param property
     * @return
     */
    public static String getString(String json, String property) {
        JSONObject jsonObject = JSONObject.parseObject(json);
        return jsonObject.getString(property);
    }
}
